/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.elicitor.core;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

/**
 *
 * @author dileepa
 */
public class PCAScale {
    
    
        public static void main(String[] args) {   
         
    
        List <Double> articulationrateScaleVal = new ArrayList<Double>(); 
        List <Double> pitchMinimumScaleVal = new ArrayList<Double>(); 
        List <Double> pitchMaximumScaleVal = new ArrayList<Double>(); 
        List <Double> pitchQ1ScaleVal = new ArrayList<Double>(); 
        List <Double> pitchQ2ScaleVal = new ArrayList<Double>(); 
        List <Double> pitchQ3ScaleVal = new ArrayList<Double>(); 
        List <Double> pitchMeanScaleVal = new ArrayList<Double>(); 
        List <Double> pitchStdScaleVal = new ArrayList<Double>(); 
        List <Double> jitterScaleVal = new ArrayList<Double>(); 
        List <Double> shimmerScaleVal = new ArrayList<Double>(); 
        List <Double> meanf1ScaleVal = new ArrayList<Double>(); 
        List <Double> meanf2ScaleVal = new ArrayList<Double>(); 
        List <Double> meanf3ScaleVal = new ArrayList<Double>(); 
        List <Double> medianf1ScaleVal = new ArrayList<Double>(); 
        List <Double> medianf2ScaleVal = new ArrayList<Double>(); 
        List <Double> medianf3ScaleVal = new ArrayList<Double>(); 
        List <Double> stdevf1ScaleVal = new ArrayList<Double>(); 
        List <Double> stdevf2ScaleVal = new ArrayList<Double>(); 
        List <Double> stdevf3ScaleVal = new ArrayList<Double>(); 
        List <Double> meansplScaleVal = new ArrayList<Double>(); 
        List <Double> mediansplScaleVal = new ArrayList<Double>(); 
        List <Double> stdsplScaleVal = new ArrayList<Double>(); 
        List <Double> minsplScaleVal = new ArrayList<Double>(); 
        List <Double> maxsplScaleVal = new ArrayList<Double>();
    
    
    
    
    


        List <Double> articulationrate = new ArrayList<Double>(); 
        List <Double> pitchMinimum = new ArrayList<Double>(); 
        List <Double> pitchMaximum = new ArrayList<Double>(); 
        List <Double> pitchQ1 = new ArrayList<Double>(); 
        List <Double> pitchQ2 = new ArrayList<Double>(); 
        List <Double> pitchQ3 = new ArrayList<Double>(); 
        List <Double> pitchMean = new ArrayList<Double>(); 
        List <Double> pitchStd = new ArrayList<Double>(); 
        List <Double> jitter = new ArrayList<Double>(); 
        List <Double> shimmer = new ArrayList<Double>(); 
        List <Double> meanf1 = new ArrayList<Double>(); 
        List <Double> meanf2 = new ArrayList<Double>(); 
        List <Double> meanf3 = new ArrayList<Double>(); 
        List <Double> medianf1 = new ArrayList<Double>(); 
        List <Double> medianf2 = new ArrayList<Double>(); 
        List <Double> medianf3 = new ArrayList<Double>(); 
        List <Double> stdevf1 = new ArrayList<Double>(); 
        List <Double> stdevf2 = new ArrayList<Double>(); 
        List <Double> stdevf3 = new ArrayList<Double>(); 
        List <Double> meanspl = new ArrayList<Double>(); 
        List <Double> medianspl = new ArrayList<Double>(); 
        List <Double> stdspl = new ArrayList<Double>(); 
        List <Double> minspl = new ArrayList<Double>(); 
        List <Double> maxspl = new ArrayList<Double>(); 
    
        
         
       
        Scanner scanner = null;
        Map <String, Object> femaleAnger= new HashMap<String, Object>();
        
        File file = new File("/home/dileepa/Documents/testValues/emotionTraingNew.csv");
 
        try {
 
             scanner = new Scanner(file);
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                String result[] = line.split(",");
        if(result.length == 24){  
            
        articulationrate.add(Double.parseDouble(result[0]));
        pitchMinimum.add(Double.parseDouble(result[1]));
        pitchMaximum.add(Double.parseDouble(result[2]));
        pitchQ1.add(Double.parseDouble(result[3]));
        pitchQ2.add(Double.parseDouble(result[4]));
        pitchQ3.add(Double.parseDouble(result[5]));
        pitchMean.add(Double.parseDouble(result[6]));
        pitchStd.add(Double.parseDouble(result[7]));
        jitter.add(Double.parseDouble(result[8]));
        shimmer.add(Double.parseDouble(result[9]));
        meanf1.add(Double.parseDouble(result[10]));
        meanf2.add(Double.parseDouble(result[11]));
        meanf3.add(Double.parseDouble(result[12]));
        medianf1.add(Double.parseDouble(result[13]));
        medianf2.add(Double.parseDouble(result[14]));
        medianf3.add(Double.parseDouble(result[15]));
        stdevf1.add(Double.parseDouble(result[16]));
        stdevf2.add(Double.parseDouble(result[17]));
        stdevf3.add(Double.parseDouble(result[18]));
        meanspl.add(Double.parseDouble(result[19]));
        medianspl.add(Double.parseDouble(result[20]));
        stdspl.add(Double.parseDouble(result[21]));
        minspl.add(Double.parseDouble(result[22]));
        maxspl.add(Double.parseDouble(result[23]));
       
        }
            }
         
        
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        finally{
            scanner.close();
        }
        
        
        double articulationMax = Collections.max(articulationrate);
        double articulationMin = Collections.min(articulationrate);
        double pitchMinimumMax = Collections.max(pitchMinimum);
        double pitchMinimumMin = Collections.min(pitchMinimum);
        double pitchMaximumMax = Collections.max(pitchMaximum);
        double pitchMaximumMin = Collections.min(pitchMaximum);
        double pitchQ1Max = Collections.max(pitchQ1);
        double pitchQ1Min = Collections.min(pitchQ1);
        double pitchQ2Max = Collections.max(pitchQ2);
        double pitchQ2Min = Collections.min(pitchQ2);
        double pitchQ3Max = Collections.max(pitchQ3);
        double pitchQ3Min = Collections.min(pitchQ3);
        double pitchMeanMax = Collections.max(pitchMean);
        double pitchMeanMin = Collections.min(pitchMean);
        double pitchStdMax = Collections.max(pitchStd);
        double pitchStdMin = Collections.min(pitchStd);
        double jitterMax = Collections.max(jitter);
        double jitterMin = Collections.min(jitter);
        double shimmerMax = Collections.max(shimmer);
        double shimmerMin = Collections.min(shimmer);
        double meanf1Max = Collections.max(meanf1);
        double meanf1Min = Collections.min(meanf1);
        double meanf2Max = Collections.max(meanf2);
        double meanf2Min = Collections.min(meanf2);
        double meanf3Max = Collections.max(meanf3);
        double meanf3Min = Collections.min(meanf3);
        double medianf1Max = Collections.max(medianf1);
        double medianf1Min = Collections.min(medianf1);
        double medianf2Max = Collections.max(medianf2);
        double medianf2Min = Collections.min(medianf2);
        double medianf3Max = Collections.max(medianf3);
        double medianf3Min = Collections.min(medianf3);
        double stdevf1Max = Collections.max(stdevf1);
        double stdevf1Min = Collections.min(stdevf1);
        double stdevf2Max = Collections.max(stdevf2);
        double stdevf2Min = Collections.min(stdevf2);
        double stdevf3Max = Collections.max(stdevf3);
        double stdevf3Min = Collections.min(stdevf3);
        double meansplMax = Collections.max(meanspl);
        double meansplMin = Collections.min(meanspl);
        double mediansplMax = Collections.max(medianspl);
        double mediansplMin = Collections.min(medianspl);
        double stdsplMax = Collections.max(stdspl);
        double stdsplMin = Collections.min(stdspl);
        double minsplMax = Collections.max(minspl);
        double minsplMin = Collections.min(minspl);
        double maxsplMax = Collections.max(maxspl);
        double maxsplMin = Collections.min(maxspl);
        
        
        calculateScale(articulationMin,articulationMax, articulationrate, articulationrateScaleVal);
        calculateScale(pitchMinimumMin,pitchMinimumMax, pitchMinimum, pitchMinimumScaleVal);
        calculateScale(pitchMaximumMin,pitchMaximumMax, pitchMaximum, pitchMaximumScaleVal);
        calculateScale(pitchQ1Min,pitchQ1Max, pitchQ1, pitchQ1ScaleVal);
        calculateScale(pitchQ2Min,pitchQ2Max, pitchQ2, pitchQ2ScaleVal);
        calculateScale(pitchQ3Min,pitchQ3Max, pitchQ3, pitchQ3ScaleVal);
        calculateScale(pitchMeanMin,pitchMeanMax, pitchMean, pitchMeanScaleVal);
        calculateScale(pitchStdMin,pitchStdMax, pitchStd, pitchStdScaleVal);
        calculateScale(jitterMin,jitterMax, jitter, jitterScaleVal);
        calculateScale(shimmerMin,shimmerMax, shimmer, shimmerScaleVal);
        calculateScale(meanf1Min,meanf1Max,meanf1, meanf1ScaleVal);
        calculateScale(meanf2Min,meanf2Max,meanf2, meanf2ScaleVal);
        calculateScale(meanf3Min,meanf3Max,meanf3, meanf3ScaleVal);
        calculateScale(medianf1Min,medianf1Max, medianf1, medianf1ScaleVal);
        calculateScale(medianf2Min,medianf2Max, medianf2, medianf2ScaleVal);
        calculateScale(medianf3Min,medianf3Max, medianf3, medianf3ScaleVal);
        calculateScale(stdevf1Min,stdevf1Max, stdevf1, stdevf1ScaleVal);
        calculateScale(stdevf2Min,stdevf2Max, stdevf2, stdevf2ScaleVal);
        calculateScale(stdevf3Min,stdevf3Max, stdevf3, stdevf3ScaleVal);
        calculateScale(meansplMin,meansplMax, meanspl, meansplScaleVal);
        calculateScale(mediansplMin,mediansplMax, medianspl, mediansplScaleVal);
        calculateScale(stdsplMin,stdsplMax, stdspl, stdsplScaleVal);
        calculateScale(minsplMin,minsplMax, minspl, minsplScaleVal);
        calculateScale(maxsplMin,maxsplMax, maxspl, maxsplScaleVal);
        
        PrintStream out = null;
        
try {
    out = new PrintStream(new FileOutputStream("pca.csv"));
    out.println("1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24");
    for (int i=0;i<930;i++){
       
       out.println(articulationrateScaleVal.get(i)+","+pitchMinimumScaleVal.get(i)+","+pitchMaximumScaleVal.get(i)+","+pitchQ1ScaleVal.get(i)+","+pitchQ2ScaleVal.get(i)+","+pitchQ3ScaleVal.get(i)+","+pitchMeanScaleVal.get(i)+","+pitchStdScaleVal.get(i)+
               ","+jitterScaleVal.get(i)+","+shimmerScaleVal.get(i)+","+meanf1ScaleVal.get(i)+","+meanf2ScaleVal.get(i)+","+meanf3ScaleVal.get(i)+","+medianf1ScaleVal.get(i)+","+medianf2ScaleVal.get(i)+","+medianf3ScaleVal.get(i)+","+stdevf1ScaleVal.get(i)+","+stdevf2ScaleVal.get(i)+","+stdevf3ScaleVal.get(i)+","+meansplScaleVal.get(i)+","+mediansplScaleVal.get(i)+","+stdsplScaleVal.get(i)+","+minsplScaleVal.get(i)+","+maxsplScaleVal.get(i)); 
    }
    
    
}catch(Exception e){
    System.err.println("Errroer "+e.getMessage());
}
finally {
    if (out != null) out.close();
}
        
        
        
    }

    

    private static void calculateScale(double minVal, double maxVal, List<Double> valList, List<Double> emptyList) {
        
        
        
        double m =0;
        double c= 0;
        double y =0;
        
        m = (1/maxVal);
        
        
//        c = 1 -m*maxVal;
       
        for (double val :valList){
            y = val*m;
            emptyList.add(y);
        }
        
        
        
    }
    
    
    
   
  
    
    
    
    
    
}


